#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Copyright © 启明星辰 版权所有
# @Time    : 2023/4/10 11:50
# @Author  : 王坤
# @File    : api.py
# @IDE     : PyCharm
# @description :
import socket

from fastapi import FastAPI, Request
from starlette.responses import FileResponse, HTMLResponse
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
from starlette.background import BackgroundTasks

app = FastAPI()

app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")

background_tasks = BackgroundTasks()

udp_socket = None


@app.on_event("startup")
async def startup():
    # 在应用程序启动时执行的代码
    print("应用程序已启动")
    global my_socket
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    udp_socket.bind(("", 7789))


@app.on_event("shutdown")
async def shutdown():
    # 在应用程序关闭时执行的代码
    udp_socket.close()

    print("应用程序正在关闭")


@app.get("/favicon.ico")
async def get_img():
    return FileResponse(f"static/favicon.ico")
